<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="出发点" prop="lpoi">
      <el-input v-model="dataForm.lpoi" placeholder="出发点"></el-input>
    </el-form-item>
    <el-form-item label="结束点" prop="rpoi">
      <el-input v-model="dataForm.rpoi" placeholder="结束点"></el-input>
    </el-form-item>
    <el-form-item label="步行时间" prop="wtime">
      <el-input v-model="dataForm.wtime" placeholder="步行时间"></el-input>
    </el-form-item>
    <el-form-item label="骑行时间" prop="rtime">
      <el-input v-model="dataForm.rtime" placeholder="骑行时间"></el-input>
    </el-form-item>
    <el-form-item label="打车时间" prop="ttime">
      <el-input v-model="dataForm.ttime" placeholder="打车时间"></el-input>
    </el-form-item>
    <el-form-item label="开车时间" prop="dtime">
      <el-input v-model="dataForm.dtime" placeholder="开车时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          lpoi: '',
          rpoi: '',
          wtime: '',
          rtime: '',
          ttime: '',
          dtime: ''
        },
        dataRule: {
          lpoi: [
            { required: true, message: '出发点不能为空', trigger: 'blur' }
          ],
          rpoi: [
            { required: true, message: '结束点不能为空', trigger: 'blur' }
          ],
          wtime: [
            { required: true, message: '步行时间不能为空', trigger: 'blur' }
          ],
          rtime: [
            { required: true, message: '骑行时间不能为空', trigger: 'blur' }
          ],
          ttime: [
            { required: true, message: '打车时间不能为空', trigger: 'blur' }
          ],
          dtime: [
            { required: true, message: '开车时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/route/pplength/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.lpoi = data.pplength.lpoi
                this.dataForm.rpoi = data.pplength.rpoi
                this.dataForm.wtime = data.pplength.wtime
                this.dataForm.rtime = data.pplength.rtime
                this.dataForm.ttime = data.pplength.ttime
                this.dataForm.dtime = data.pplength.dtime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/route/pplength/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'lpoi': this.dataForm.lpoi,
                'rpoi': this.dataForm.rpoi,
                'wtime': this.dataForm.wtime,
                'rtime': this.dataForm.rtime,
                'ttime': this.dataForm.ttime,
                'dtime': this.dataForm.dtime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
